<?php
/**
 * 金币模型
 * @return
 * @author yuncran@126.com
 */

namespace app\model;

use app\BaseModel;
use app\CacheKey;

class GoldModel extends BaseModel
{

    protected $pk = "id";

    protected $name = "gold";

    /**
     * 获取单条金币
     * @param $where
     * @return array|mixed|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author yuncran@126.com
     */
    public static function getOne($where = [])
    {
        return self::getModel()->where($where)->find();
    }

    /**
     * 获取金币列表
     * @return array|null[]|\think\Collection|\think\Model[]
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * @author yuncran@126.com
     */
    public static function getGoldList()
    {
        $user_level = UserLevelModel::where('id','>',1)->field(['id',field('title'),'describe','image','money','expire','color','font_color','background_image','create_time'])->cache(true, CacheKey::gold_list)->select();
        if (!empty($user_level)) {
            foreach ($user_level as $key => $value) {
                $user_level[$key]['list'] = self::getModel()->where(['status' => 1])->where(['type' => $value['id']])->cache(true, CacheKey::gold_list)->select();
            }
        }
        return empty($user_level) ? [] : $user_level;
    }

}